<template>
  <div class="comment-list">
    評論
    <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
    >
      <comment-item v-for="(item,index) in list" :comment="list" :key="index" :title="item.content" />
    </van-list>
  </div>
</template>

<script>
import {getComments} from "@/api/comment";
import commentItem from "@/views/article/components/commentItem";

export default {
  name: "commentList",
  components:{
    commentItem
  },
  props:{
    source:{
      type:[Number,String,Object],
      required:true
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      offset:null,
      limit:10
    };
  },
  methods: {
    async onLoad() {
      const {data}=await getComments({
        type:'a',
        source:this.source,
        offset:this.offset,
        limit:this.limit
      })
      console.log(data);
      const {results}=data.data
      this.list.push(...results)
      this.loading=false
      if (results.length){
        this.offset=data.data.last_id
      }else {
        this.finished=true
      }
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求

    }
  }
}
</script>

<style scoped lang="less">
.comment-list{
  background-color: #ffffff;
  margin: 0;
}
</style>